//
//  MNavBar.swift
//  MM2
//
//  Created by 李成明 on 2022/1/5.
//

import UIKit

class MNavBar: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addsubViews([backIcon, titleLabel])
        backIcon.frame = CGRect(x: 16, y: 8, width: 28, height: 28)
        titleLabel.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kNaviBarHeight)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var backClosure: (() -> Void)?
    var title = "页面" {
        didSet {
            titleLabel.text = title
        }
    }
    
    private lazy var backIcon: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "fanhui")
        
        view.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(backAction))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont(name: "Futura-Bold", size: 20)
        view.textAlignment = .center
        return view
    }()
    
    @objc private func backAction() {
        backClosure?()
    }
    
    @discardableResult
    func back(_ closure: (() -> Void)?) -> MNavBar{
        backClosure = closure
        return self
    }
}
